package top.yudoge.dogeeditor.llm.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import top.yudoge.dogeeditor.llm.config.models.ModelRegistry
import top.yudoge.dogeeditor.llm.config.models.QWenModelInitializer

@Configuration
class LargeLanguageModelDynamicConfig {

    @Bean
    fun modelRegistry(properties: LargeLanguageModelsProperties): ModelRegistry {
        val registry = ModelRegistry()

        properties.models.forEach { model ->
            val modelInitializer = modelInitializers.firstOrNull() { it.support(model.modelName) }
                ?: throw IllegalArgumentException("Unsupported model name: ${model.modelName}")

            val streamModel = modelInitializer.initializeStreamModel(model)
            val chatModel = modelInitializer.initializeChatModel(model)
            registry.registerStreamingChatModel(model.modelName, streamModel)
            registry.registerChatModel(model.modelName, chatModel)
        }

        return registry
    }

    companion object {
        private val modelInitializers = listOf(QWenModelInitializer())
    }

}